package gui;

import domain.DomainController;
import domain.player.*;
import java.awt.event.*;
import javax.swing.JLabel;

/**
 * HumanListener handles a player's (Human type) mouse clicks, representing
 * their next move.
 */
public class HumanListener extends MouseAdapter {

	private DomainController domainController;

	public HumanListener(DomainController domainController) {
		this.domainController = domainController;
	}

	/** Client clicked the game board */
	@Override
	public void mouseClicked(MouseEvent e) {
		Player player = domainController.getCurrentPlayer();

		// Ignore mouse click (not a Human):
		if (!(player instanceof Human)) {
			return;
		}

		Human humanPlayer = (Human) player;

		try {
			int column = Integer.parseInt(((JLabel) e.getSource()).getName());
			humanPlayer.setNextMove(column);
		} catch (NumberFormatException ex) {
			System.out.println("Faulty column value passed to parseInt: " + ex);
		} catch (ClassCastException ex) {
			System.out.println("Typecast failed: " + ex);
		}
	}
}
